﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Imaginecup.sprite.animation
{
    public class AnimationIndexMan : IAnimationIndexProvider, IControlAnimationIndex
    {
        private double m_DelaySecond;
        private double m_CurrentSecond;

        private int m_MaxCount;
        private int m_CurrentCount;

        private Vector2 m_CurrentIndex;
        private Vector2 m_StartIndex;

        private bool m_bAnimation;
        private int m_AnimationCycleCount;
        private bool m_bLastIndex;

        public AnimationIndexMan()
        {
            //초기 애니메이션 상태 아님.
            m_bAnimation = false;
            m_CurrentSecond = 0;
        }

        public int GetCycleCount()
        {
            return m_AnimationCycleCount;
        }

        public bool IsLastIndex()
        {
            return m_bLastIndex;
        }

        public void SetDelay(double delaySecond)
        {
            m_DelaySecond = delaySecond;
        }

        public void SetIndex(Vector2 startIndex, int maxCount)
        {
            m_MaxCount = maxCount;
            m_CurrentCount = 0;
            m_StartIndex = startIndex;
            m_CurrentIndex = m_StartIndex;
        }

       
        public void StartAnimation()
        {
            m_bLastIndex = false;
            m_bAnimation = true;
            m_AnimationCycleCount = 0;
        }

        public void StopAnimation()
        {
            m_bAnimation = false;
        }

        public Vector2 GetAnimationIndex()
        {
            return m_CurrentIndex;
        }


        private int GetLastIndexNum()
        {
            return m_MaxCount - 1;
        }

        public void Update(GameTime gameTime)
        {
            if (m_bAnimation == false) return;

            m_CurrentSecond += gameTime.ElapsedGameTime.TotalSeconds;

            if (m_CurrentSecond > m_DelaySecond)
            {
                m_CurrentSecond = m_CurrentSecond - m_DelaySecond;

                AddIndexProcess();
            }
        }

        private void AddIndexProcess()
        {
            m_CurrentIndex.X ++;
            m_CurrentCount++;

            if (m_CurrentCount == m_MaxCount)
            {
                m_CurrentCount = 0;
                m_AnimationCycleCount++;

                m_CurrentIndex.X = m_StartIndex.X;
                m_bLastIndex = false;
            }

            if (m_CurrentCount == m_MaxCount - 1)
            {
                m_bLastIndex = true;
            }
        }
    }
}












 //private void AddIndexWithPercentProcess()
 //       {
 //           m_CurrentIndex.X = m_StartIndex.X + m_CurrentCount;

 //           if (m_CurrentCount == m_MaxCount - 1)
 //           {
 //               m_bLastIndex = true;
 //           }
 //       }





  //public void AddIndexWithPercent(double percent)
  //      {
  //          if (m_bLastIndex) return;


  //          int indexCount = (int)(GetLastIndexNum() * percent / 100.0);

  //          int increaseNum = indexCount - m_CurrentCount;
  //          m_CurrentCount += increaseNum;// 차이만큼 더한다. 그렇지 않고 단순히 indexCount를 더하면 값이 누적해서 더해지게 된다.

  //          if( m_CurrentCount > GetLastIndexNum() )
  //          {
  //              m_CurrentCount = GetLastIndexNum();

  //          }

  //          AddIndexWithPercentProcess();
  //      }